package com.tianxiang.action;

import java.text.DecimalFormat;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tianxiang.dao.UserDao;
import com.tianxiang.model.User;
import com.tianxiang.service.ManageFileService;
import com.tianxiang.utils.Tools;

public class UserManageAction extends ActionSupport {

	private int userId;
	private String tip;

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}



	private static final long serialVersionUID = 1L;


	public String getUsers(){
		List<User> userlist = new UserDao().getAllUsers();
		System.out.println("------------执行getUsers方法");
		DecimalFormat numFormat = new DecimalFormat();
		String format = "00.00"; // 必须以0为形式，其他数字保持不变
		numFormat.applyPattern(format);
		for (User user:userlist) {
			//User user = (User) iterator.next();
			String percent=numFormat.format(100*user.getUsedspace() / user.getTotalspace());
			user.setUsedspace(Float.parseFloat(percent));
			user.setTotalspace(Tools.getFileSize((long) user.getTotalspace()));
		}
		ActionContext.getContext().getSession().put("userlist", userlist);
		return SUCCESS;
	}

	/**
	 * 删除用户
	 * 
	 * @return
	 */
	public String delUser() {
		//int id=Integer.parseInt(userId);
		//boolean result = new UserDao().delUser(userId);
		UserDao dao=new UserDao();
	//	User user=dao.findUserById(userId);
		boolean result=dao.delUser(userId);
		System.out.println("删除用户"+userId);
		if (result) {
	//		new ManageFileService().delete(Tools.getUserPath(user.getUsername()));		//删除用户文件夹
			return SUCCESS;
		}
		tip = "删除用户失败";
		return "fail";
	}

	public String resetPwd() {
		//int id=Integer.parseInt(userId);
		System.out.println("重置用户密码"+userId);
		UserDao dao = new UserDao();
		User user = dao.findUserById(userId);
		if (user==null) {
			tip="删除失败！";
			return "fail";
		}
		user.setPassword(Tools.makeMD5(user.getUsername())); // 将密码重置为用户名
		dao.update(user);
		tip="已将用户密码重置为其用户名";
		return SUCCESS;
	}
	
	public static void main(String[] args) {
		UserManageAction userManageAction=new UserManageAction();
		userManageAction.setUserId(16);
		System.out.println(userManageAction.resetPwd());
	}
}
